<template>
  <el-aside :width="isCollapse ? '64px' : '200px'">
    <el-scrollbar wrap-class="scrollbar-wrapper">
      <el-menu
        :default-active="activeMenu"
        background-color="#001529"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
        :collapse="isCollapse"
        :collapse-transition="false"
        router
        unique-opened
      >
        <sidebar-item
          v-for="route in processedRoutes"
          :key="route.path"
          :item="route"
          :base-path="route.path"
        />
      </el-menu>
    </el-scrollbar>
  </el-aside>
</template>

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import SidebarItem from './SidebarItem.vue'
import { usePermissionStore } from '@/stores/permission'

const permissionStore = usePermissionStore()
const route = useRoute()

// 修改processedRoutes计算属性
const processedRoutes = computed(() => {
  if (!permissionStore.isReady) return [] // 等待路由就绪
  
  const filter = (routes) => routes.filter(route => {
    const valid = !route.meta?.hidden && 
                 (route.component || route.children?.length)
    return valid
  }).map(route => ({
    ...route,
    children: route.children ? filter(route.children) : []
  }))
  
  return filter(permissionStore.routes)
})

// 添加加载状态提示
<template>
  <el-aside ...>
    <el-scrollbar>
      <template v-if="!permissionStore.isReady">
        <div class="loading-text">路由加载中...</div>
      </template>
      <el-menu v-else>
        ...原有内容...
      </el-menu>
    </el-scrollbar>
  </el-aside>
</template>

<style>
.loading-text {
  color: #fff;
  padding: 20px;
  text-align: center;
}
</style>

// 当前激活菜单计算
const activeMenu = computed(() => {
  return route.meta.activeMenu || route.path
})

// 接收折叠状态
const props = defineProps({
  isCollapse: {
    type: Boolean,
    default: false
  }
})

// 在setup顶部添加调试代码
import { onMounted } from 'vue'

onMounted(() => {
  console.log('当前权限路由:', permissionStore.routes)
  console.log('处理后路由:', processedRoutes.value)
})
</script>

<style lang="scss" scoped>
.el-aside {
  transition: width 0.3s ease;
  
  .el-menu {
    border-right: none;
    
    &:not(.el-menu--collapse) {
      width: 200px;
    }
  }
}
</style>